// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: FZ Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 76 : TYPE_FZEGGMAN

// Function declarations
reserve function FZEggman_Function158

// Tables
public table FZEggman_table49
	8, 4, 9, 4
end table

public table FZEggman_table48
	1, 2, 2, 3, 3, 4, 4, 1
end table


function FZEggman_Function158
	CheckEqual(object[currentPlayer].animation, ANI_JUMPING)
	temp0 = checkResult
	CheckEqual(object[currentPlayer].animation, ANI_GLIDING)
	temp0 |= checkResult
	CheckEqual(object[currentPlayer].animation, ANI_GLIDING_STOP)
	temp0 |= checkResult
	if temp0 == 1
		object[currentPlayer].xvel = 0x30000
		if object[currentPlayer].xpos < object.xpos
			FlipSign(object[currentPlayer].xvel)
		end if
		object[currentPlayer].speed = object[currentPlayer].xvel
		if object[currentPlayer].animation == ANI_GLIDING
			object[currentPlayer].animation = ANI_GLIDING_DROP
			object[currentPlayer].state = PlayerObject_KnuxGlideDrop
		end if
		checkResult = 1
	else
		checkResult = 0
	end if
end function


event ObjectMain
	switch object.state
	case 0
		temp1 = screen.xcenter
		temp2 = temp1
		if temp1 == 160
			temp1 += 16
		end if
		temp0 = object.ixpos
		temp0 -= temp1
		stage.newXBoundary1 = temp0
		temp0 += temp1
		temp0 += temp2
		stage.newXBoundary2 = temp0
		temp0 = object.entityPos
		object[+1].value0 = temp0
		object[+2].value0 = temp0
		object[+3].value0 = temp0
		object[+4].value0 = temp0
		object.value3 = object.ixpos
		object.value3 += 656
		object.value5 = object.ypos
		object.value5 -= 0x400000
		object.state++
		break
	case 1
		if object[0].xpos >= object.xpos
			object.xpos = 0
			object.ypos = 0
			object.value4 = 1
			object.state++
		end if
		break
	case 2
		object.animation = 1
		Rand(temp0, 8)
		temp1 = temp0
		temp1 &= 1
		temp0 &= 6
		GetTableValue(temp2, temp0, FZEggman_table48)
		temp0++
		GetTableValue(temp3, temp0, FZEggman_table48)
		arrayPos0 = object.entityPos
		arrayPos0 += temp2
		object[arrayPos0].state = 1
		if temp1 == 0
			object[arrayPos0].propertyValue = 1
		end if
		arrayPos0 = object.entityPos
		arrayPos0 += temp3
		object[arrayPos0].state = 1
		if temp1 == 1
			object[arrayPos0].propertyValue = 1
		end if
		PlaySfx(SfxName[Large Wall], 0)
		object.value0 = 160
		object.state++
		break
	case 3
		object.value0--
		if object.value0 <= 0
			if object.value2 == 8
				temp1 = screen.xcenter
				temp0 = object.value3
				temp0 -= temp1
				stage.newXBoundary1 = temp0
				temp0 += temp1
				temp0 += temp1
				stage.newXBoundary2 = temp0
				object.xpos = object[+5].xpos
				object.xpos += 0x380000
				object.ypos = object[+5].ypos
				object[+7].value6 = object.value3
				object[+7].value6 <<= 16
				object.direction = FLIP_X
				arrayPos0 = object.entityPos
				arrayPos0 += 5
				ResetObjectEntity(arrayPos0, TypeName[Explosion], 0, object[arrayPos0].xpos, object[arrayPos0].ypos)
				object.value4 = 0
				object.animation = 4
				object.state = 6
			else
				object.state++
			end if
		end if
		break
	case 4
		object[+5].state = 1
		object[+5].value0 = 330
		temp0 = 0
		temp1 = 0
		while temp0 < 4
			CreateTempObject(TypeName[Plasma Ball], 0, object[+5].xpos, object[+5].ypos)
			object[tempObjectPos].inkEffect = INK_ADD
			object[tempObjectPos].alpha = 0
			object[tempObjectPos].value1 = 4
			temp0++
			object[tempObjectPos].value1 = temp1
			object[tempObjectPos].value1 += object[+5].xpos
			object[tempObjectPos].value1 -= 0x100000
			Rand(temp2, 32)
			temp2 -= 16
			temp2 <<= 16
			object[tempObjectPos].value1 += temp2
			temp1 -= 0x4F0000
		loop
		object.value0 = 330
		object.state++
		break
	case 5
		object.value0--
		if object.value0 <= 0
			object.state = 2
		end if
		break
	case 6
		object.ypos += object.yvel
		object.yvel += 0x1000
		if object.ypos >= object.value5
			ObjectTileCollision(CSIDE_FLOOR, 0, 28, 0)
			if checkResult == 1
				object.animation = 0
				object.xvel = 0x10000
				object.yvel = -0x10000
				object.value0 = 0
				object.animation = 5
				object.state++
			end if
		end if
		break
	case 7
		object.xpos += object.xvel
		object.ypos += object.yvel
		ObjectTileCollision(CSIDE_FLOOR, 0, 28, 0)
		if checkResult == 0
			object.yvel += 0x1000
		else
			object.yvel = 0
		end if
		temp1 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos > temp1
				temp1 = object[currentPlayer].xpos
			end if
		next
		temp0 = object.xpos
		temp0 -= temp1
		if temp0 < 0
			object.xvel = 0x50000
		else
			object.xvel = 0x40000
			temp0 -= 0x700000
			if temp0 >= 0
				object.xvel -= 0x10000
				temp0 -= 0x80000
				if temp0 >= 0
					object.xvel -= 0x10000
					temp0 -= 0x80000
					if temp0 >= 0
						object.xvel -= 0x8000
						temp0 -= 0x80000
						if temp0 >= 0
							object.xvel -= 0x8000
							temp0 -= 0x80000
							if temp0 >= 0
								object.xvel -= 0x8000
								temp0 -= 0x380000
								if temp0 >= 0
									object.xvel = 0
								end if
							end if
						end if
					end if
				end if
			end if
		end if
		temp0 = object[+7].xpos
		temp0 -= 0x300000
		if object.xpos >= temp0
			object.xvel = 0x24000
			object.yvel = -0x4C000
			object.state++
		end if
		break
	case 8
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3400
		if object.xpos >= object[+7].xpos
			object.xpos = object[+7].xpos
			object.xvel = 0
		end if
		if object.yvel > 0
			if object.ypos >= object[+7].ypos
				object.ypos = object[+7].ypos
				object.yvel = 0
				object[+7].state++
				object.type = TypeName[Blank Object]
			end if
		end if
		break
	end switch
	switch object.animation
	case 0
		object.frame = 0
		break
	case 1
		object.animationTimer++
		if object.animationTimer == 14
			object.animationTimer = 0
		end if
		object.frame = object.animationTimer
		object.frame /= 7
		object.frame++
		break
	case 2
		if object.yvel == 0
			object.frame = 3
		else
			object.frame = 4
		end if
		break
	case 3
		object.frame = object.value1
		object.frame &= 1
		object.frame += 5
		break
	case 4
		object.frame = 7
		break
	case 5
		object.value0++
		if object.value0 >= 28
			object.value0 = 0
		end if
		temp0 = object.value0
		temp0 /= 7
		GetTableValue(object.frame, temp0, FZEggman_table49)
		break
	end switch
	if object.value1 != 0
		object.value1--
	end if
	if object.value4 == 1
		if object[0].xpos < object.xpos
			object.direction = FLIP_NONE
		else
			object.direction = FLIP_X
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -33, -37, 33, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(FZEggman_Function158)
				if checkResult == 1
					if object.value1 == 0
						if object.value2 < 8
							object.animation = 3
							object.value1 = 100
							PlaySfx(SfxName[Boss Hit], 0)
							object.value2++
						end if
					end if
				end if
			end if
		next
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = object[currentPlayer].collisionRight
		temp0 <<= 16
		temp0 += object[currentPlayer].xpos
		temp1 = stage.curXBoundary2
		temp1 <<= 16
		if temp0 > temp1
			object[currentPlayer].xvel = 0
			object[currentPlayer].speed = 0
			object[currentPlayer].xpos = temp1
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			object[currentPlayer].xpos -= temp0
		end if
	next
end event


event ObjectDraw
	if object.state >= 3
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-10, -23, 38, 51, 1, 209)
	SpriteFrame(-10, -23, 32, 54, 50, 295)
	SpriteFrame(-10, -24, 32, 54, 84, 261)
	SpriteFrame(-10, -23, 32, 54, 84, 316)
	SpriteFrame(-10, -16, 46, 44, 350, 165)
	SpriteFrame(-10, -23, 46, 55, 350, 210)
	SpriteFrame(-10, -24, 32, 54, 192, 266)
	SpriteFrame(-10, -24, 60, 44, 289, 165)
	SpriteFrame(-10, -23, 58, 44, 234, 221)
	SpriteFrame(-10, -25, 51, 52, 269, 213)
	SpriteFrame(-10, -20, 42, 64, 225, 319)
	foreach (TypeName[FZ Eggman], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
